package com.example.mqttmodule.controller;

import com.example.mqttmodule.service.PublishService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mqtt")
@Log4j2
public class MqttTestController {

    @Autowired
    private PublishService publisher;

    @PostMapping("/send")
    public String send(@RequestBody String message) {
        try {
            publisher.publish("/test/123", message);
        } catch (Exception e) {
            log.error("Error sending message", e);
            return "Error sending message " + message + ": " + e.getMessage();
        }
        return "Message sent" + message;
    }
}